<template>
  <edit-dialog :centerDialogVisible="visible" :tilte="title" width="600" @Cancel="handleCancel" :defaultButton="false">
    <template #content>
      <el-form :model="form" :rules="rules" ref="applyApproveFormRef" class="upload-approve-form">
        <ApplyApproveUploadFile v-if="formType === ApplyApproveType.UPLOAD" :form="form" disabled/>
        <ApplyApproveSafeEvaluate v-if="formType === ApplyApproveType.SAFE_EVALUATE" :form="form" disabled/>
        <el-form-item v-if="formType === ApplyApproveType.DATE" label="申请开工时间：" prop="exeContent.time" disabled>
<!--          <el-date-picker :value="form.exeContent?.formContent?.date || form.exeContent.time" value-format="YYYY-MM-DD"></el-date-picker>-->
          <span>{{form.exeContent?.formContent?.date || form.exeContent.time}}</span>
        </el-form-item>
        <el-form-item v-if="form.isReject" label="审批意见" prop="examineOpinion">
          <el-input v-model="form.examineOpinion" maxlength="200" show-word-limit/>
        </el-form-item>
      </el-form>
    </template>
    <template #custom>
      <template v-if="form.isReject">
        <el-button @click="closeRejectReason">取消</el-button>
        <el-button type="danger" @click="handleReject" :disabled="rejectLoading">驳回</el-button>
      </template>
      <template v-else>
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="danger" @click="showRejectReason">驳回</el-button>
        <el-button type="primary" @click="handleSubmit" :disabled="loading">通过</el-button>
      </template>
    </template>
  </edit-dialog>
</template>

<script>
import EditDialog from "@/components/editDialog/index.vue";
import ApplyApproveUploadFile from "@/views/construction/project-application/review-components/ApplyApproveUploadFile";
import ApplyApproveSafeEvaluate from "@/views/construction/project-application/review-components/ApplyApproveSafeEvaluate";
import {ApplyApproveType} from "@/views/construction/project-application/projectUtil";
import {approveBeforeProjectStart} from "@/api/construction-project/projectManage";
import store from "@/store";

export default {
  name: "ApplyApproveDialog",
  components: {ApplyApproveUploadFile,ApplyApproveSafeEvaluate, EditDialog},
  props: {
    options: {
      type: Object,
      default: () => ({})
    }
  },
  inject: ["options"],
  data() {
    return {
      visible: false,
      form: {
        type: Object,
        default: () => ({
          exeContent: {
            files: [],
            formContent: {}
          },
        })
      },
      rules: {
        // 'exeContent.time': [
        //   {required: true, message: '请选择', trigger: ['change']},
        // ],
        'examineOpinion': [
          {required: true, message: '请输入', trigger: ['change']},
        ],
      },
      title: '添加外部人员',
      formType: undefined,
      ApplyApproveType,
      rejectLoading: false,
      loading: false
    }
  },
  computed: {
    userInfo() {
      return store.state.user.userInfo
    },
  },
  methods: {
    open({row, title, formType, resolve}) {
      this.resolve = resolve
      this.visible = true
      this.title = title
      this.formType = formType
      this.form = {
        status: 1,
        currentNodeId: row.currentNodeId,
        approver: {
          id: this.userInfo.userId,
          name: this.userInfo.name,
          organId: this.userInfo.organId,
          organName: this.userInfo.organName,
        },
        projectId: row.id,
        change: false,
        exeContent: row?.preNodeContent || {},
      }
    },
    handleCancel() {
      this.visible = false
    },
    showRejectReason() {
      this.form.status = 0
      this.form.isReject = true
    },
    closeRejectReason() {
      this.form.status = 1
      this.form.isReject = false
    },
    handleSubmit() {
      let that = this
      this.$refs.applyApproveFormRef.validate(valid => {
        if (valid) {
          this.loading=true
          let {exeContent, ...more} = that.form
          approveBeforeProjectStart(more).then(res => {
            that.$message.success("操作成功")
            that.resolve()
            that.visible = false
          }).catch(e => {
            this.$message.error(e)
          }).finally(()=>{
            this.loading=false
          })
        }
      })
    },
    handleReject() {
      this.$refs.applyApproveFormRef.validate(valid => {
        if (valid) {
          this.rejectLoading = true
          approveBeforeProjectStart(this.form).then(res => {
            this.visible = false
            this.resolve()
          }).catch(e => {
            this.$message.error(e)
          }).finally(()=>{
            this.rejectLoading = false
          })
        }
      })
    },
  }
}
</script>

<style lang="less" scoped>
.upload-approve-form {

}
</style>
